import 'dart:collection';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/http/http_utils.dart';
import 'package:tender_flutter/page/home/model/common_tag_model.dart';
import 'package:tender_flutter/page/home/model/company_entity.dart';
import 'package:tender_flutter/page/home/model/market_detail_entity.dart';
import 'package:tender_flutter/page/home/model/market_entity.dart';
import 'package:tender_flutter/page/home/model/tender_detail_entity.dart';

class ApiHome {
  /// 获取热门搜索关键词
  static Future getHotKeywords<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.hotKeywords);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 公告
  static Future getAnnouncement<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.announcement);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取每天更新的总标新信息数量
  static Future getTenderNum<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.tenderNum);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取招标公告列表
  /// type   1是招标  2是中标 3是铁路招标  4是铁路招标  5是军队招标  6军队中标 (参见 Constant)
  /// state  1集中采购  2部队采购
  /// isTerm  是否根据keyword精确查找
  /// beginTime 开始时间（比如 2020-10-01）
  /// endTime 结束时间（比如 2020-10-01）
  /// dynamicParams 动态参数，即key和value独食动态的
  static Future getTenderList<T>(
      {int areaCode,
      int state,
      String keyword,
      String type,
      bool isTerm = false,
      String beginTime,
      String endTime,
      String maxMoney,
      String minMoney,
      Map<String, dynamic> dynamicParams,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["area_id"] = (areaCode != null && areaCode != 0) ? areaCode : null;
      params["begin"] = beginTime;
      params["end"] = endTime;
      params["jd_state"] = state;
      params["is_term"] = isTerm;
      params["money_max"] = maxMoney;
      params["money_min"] = minMoney;
      params["title"] = keyword;
      params["type"] = type;
      params["page"] = page;
      params["size"] = pageCount;
      if (dynamicParams != null) {
        params.addAll(dynamicParams);
      }

      final response =
          await HttpUtils.postForm(ApiPath.tenderList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 查询标讯详情
  /// id  标讯id
  static Future<ApiResponse<TenderDetailEntity>> getTenderDetail(
      int tenderId) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = tenderId;
      final response =
          await HttpUtils.get(ApiPath.tenderDetail, params: params);
      return ApiResponse<TenderDetailEntity>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 查询企业
  /// id   行业类别
  static Future<ApiResponse<CompanyEntity>> getCompany(
      {int areaCode,
      String keyword,
      int id,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      if (areaCode != null && areaCode != 0) {
        params["areaCode"] = areaCode;
      }
      params["title"] = keyword;
      params["id"] = id;
      params["page"] = page;
      params["size"] = pageCount;

      final response = await HttpUtils.get(ApiPath.companyList, params: params);
      return ApiResponse<CompanyEntity>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 企业详情
  /// companyKey  企业key
  /// companyName  公司名称
  static Future companyDetail<T>(
      {int type, String companyKey, String companyName}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["companyKey"] = companyKey;
      params["companyName"] = companyName;
      print(params.toString());
      final response =
          await HttpUtils.get(ApiPath.companyDetail, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 对外投资
  /// companyKey  企业key
  /// companyName  公司名称
  static Future companyInvested<T>(
      {String companyKey, String companyName}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["companyKey"] = companyKey;
      params["companyName"] = companyName;
      print(params.toString());
      final response =
          await HttpUtils.get(ApiPath.companyInvested, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 分支机构
  /// companyKey  企业key
  /// companyName  公司名称
  static Future companyMaster<T>(
      {String companyKey,
      String companyName,
      int page = AppConfig.page}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["companyKey"] = companyKey;
      params["companyName"] = companyName;
      params["page"] = page;
      print(params.toString());
      final response =
          await HttpUtils.get(ApiPath.companyMaster, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 公司变更信息
  /// companyKey  企业key
  /// companyName  公司名称
  static Future companyChange<T>(
      {String companyKey,
      String companyName,
      int page = AppConfig.page}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["companyKey"] = companyKey;
      params["companyName"] = companyName;
      params["page"] = page;
      print(params.toString());
      final response =
          await HttpUtils.get(ApiPath.companyChange, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 失信查询
  /// name  名称
  /// code  编码
  static Future creditSearch<T>(
      {String name,
      String code,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["name"] = name;
      params["num"] = code;
      params["page"] = page;
      print(params.toString());
      final response =
          await HttpUtils.get(ApiPath.creditSearch, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 供需查询
  /// type   行业类别
  static Future<ApiResponse<MarketEntity>> getMarketList(
      {int areaCode,
      String keyword,
      String beginTime,
      String endTime,
      int type,
      int industryId,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["area_id"] = areaCode;
      params["type"] = type;
      params["infoType"] = industryId == 0 ? null : industryId;
      params["begin"] = beginTime;
      params["end"] = endTime;
      params["title"] = keyword;
      params["page"] = page;
      params["size"] = pageCount;
      final response = await HttpUtils.get(ApiPath.marketList, params: params);
      return ApiResponse<MarketEntity>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 供需详情
  /// id  标讯id
  static Future<ApiResponse<MarketDetailEntity>> getMarketDetail(
      int marketId) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = marketId;
      final response =
          await HttpUtils.get(ApiPath.marketDetail, params: params);
      return ApiResponse<MarketDetailEntity>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取供需联系方式
  static Future getMarketContact<T>(int marketId) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["market_id"] = marketId;
      final response =
          await HttpUtils.get(ApiPath.marketContact, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 供需行业类别
  /// id  标讯id
  static Future getMarketIndustryCategory<T>() async {
    try {
      final response = await HttpUtils.post(ApiPath.marketIndustryCategory);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 发布供需
  static Future publishMarket<T>(
      {String title,
      String userName,
      String company,
      int areaCode,
      int infoType,
      int type,
      String phone,
      String price,
      String content,
      List<String> pics,
      String wxAccount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["area_id"] = areaCode;
      params["company"] = company;
      params["contact"] = userName;
      params["content"] = content;
      params["infoType"] = infoType;
      params["money"] = price;
      params["phone"] = phone;
      params["pics"] = pics;
      params["title"] = title;
      params["type"] = type;
      params["wxAccount"] = wxAccount;

      final response =
          await HttpUtils.postRaw(ApiPath.publishMarket, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 资质类别
  static Future getCertificateCategory<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.certificateCategory);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 资质证书列表
  static Future getCertificateList<T>({
    String time,
    String categoryId,
    String companyName,
    int page = AppConfig.page,
  }) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["range"] = time;
      params["cat"] = categoryId;
      params["companyName"] = companyName;
      params["page"] = page;
      final response =
          await HttpUtils.get(ApiPath.certificateList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 资质详情
  static Future getCertificateDetail<T>({
    String certId,
  }) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["certId"] = certId;
      final response =
          await HttpUtils.get(ApiPath.certificateDetail, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取置顶套餐
  static Future getPlaceTopCombo<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.topCombo);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 权鸟资讯列表
  /// type 1 : 权鸟动态， 2： 行业前沿
  static Future getArticleList<T>(
      {int type,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["type"] = type;
      params["page"] = page;
      params["size"] = pageCount;
      final response = await HttpUtils.get(ApiPath.articleList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 权鸟资讯详情
  static Future getArticleDetail<T>({int id}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = id;
      final response =
          await HttpUtils.get(ApiPath.articleDetail, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取筛选菜单中的金额列表
  static Future<List<CommonTagModel>> getFilterMoneyDatas() async {
    List<CommonTagModel> datas = [
      CommonTagModel(title: '全部', value: ",", isSelected: true),
      CommonTagModel(title: '0-2万', value: "1,20000", isSelected: false),
      CommonTagModel(title: '2-5万', value: "20000,50000", isSelected: false),
      CommonTagModel(title: '5-20万', value: "50000,200000", isSelected: false),
      CommonTagModel(
          title: '20-50万', value: "200000,500000", isSelected: false),
      CommonTagModel(
          title: '50-100万', value: "500000,1000000", isSelected: false),
      CommonTagModel(
          title: '100-500万', value: "1000000,5000000", isSelected: false),
      CommonTagModel(
          title: '500-1000万', value: "5000000,10000000", isSelected: false),
      CommonTagModel(
          title: '1000万以上', value: "10000000,100000000", isSelected: false)
    ];
    return datas;
  }

  /// 获取筛选菜单中的时间列表
  static Future<List<CommonTagModel>> getFilterTimesDatas() async {
    var today = DateTime.now();
    var formats = [yyyy, '-', mm, '-', dd, " ", HH, ":", nn, ":", ss];
    var curTime = formatDate(today, formats);
    var time1 = formatDate(today.subtract(Duration(days: 30)), formats);
    var time2 = formatDate(today.subtract(Duration(days: 7 * 30)), formats);
    var time3 = formatDate(today.subtract(Duration(days: 12 * 30)), formats);
    var time4 = formatDate(today.subtract(Duration(days: 24 * 30)), formats);
    var time5 = formatDate(today.subtract(Duration(days: 120 * 30)), formats);
    List<CommonTagModel> timeDatas = [
      CommonTagModel(title: '全部', value: ",", isSelected: true),
      CommonTagModel(
          title: '1个月内', value: time1 + "," + curTime, isSelected: false),
      CommonTagModel(
          title: '7个月内', value: time2 + "," + curTime, isSelected: false),
      CommonTagModel(
          title: '1年内', value: time3 + "," + curTime, isSelected: false),
      CommonTagModel(
          title: '2年内', value: time4 + "," + curTime, isSelected: false),
      CommonTagModel(
          title: '2年以上', value: time5 + "," + curTime, isSelected: false),
    ];
    return timeDatas;
  }
}
